// reducer + combinReducers
import * as ActionTypes from './action-types';
// 报错：但从未读取其值 - 后面没用到，引入可能没必要，是垃圾代码
// ts ： 类型检测 代码准确性检测
// tree-sharking：前端在打包时,去掉不需要的代码
import { combineReducers, AnyAction } from 'redux';

const initalState = {
    loading: true,
    home: {
        partitions: {},
    },
    search: {
        suggest: [],
        hotword: [],
        searchResult: {}
    }
    // 首页数据
    // 视频页数据
    // 登录页数据...
}

const combineHome = (state = initalState.home, action: AnyAction) => {
    switch (action.type) {
        case ActionTypes.SET_PARTITIONS:
            return {
                ...state,
                partitions: action.data
            }
        default:
            return state
    }
}

const combineSearch = (state = initalState.search, action: AnyAction) => {
    switch (action.type) {
        case ActionTypes.SET_HOTWORD:
            return {
                ...state,
                hotword: action.data
            }
        case ActionTypes.SET_SUGGEST:
            return {
                ...state,
                suggest: action.data
            }
        case ActionTypes.SET_SEARCH_RESULT:
            return {
                ...state,
                searchResult: action.data
            }
        default:
            return state
    }
}

const combineLoading = (state = initalState.loading, action: AnyAction) => {
    switch (action.type) {
        default:
            return state;
    }
}


export default combineReducers({
    search: combineSearch,
    loading: combineLoading,
    home: combineHome,
})